package com.wyx.springvueadmin.controller;
import com.wyx.springvueadmin.entity.PageBean;
import com.wyx.springvueadmin.entity.Result;
import com.wyx.springvueadmin.entity.User;
import com.wyx.springvueadmin.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class userController {
    @Autowired
    private userService userservice;
    /**
     * 新增
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userservice.save(user);
        return Result.success();
    }
    /**
     * 查询全部
     * @return
     */
    @GetMapping ("/")
    public Result findAll(){
        List<User> list=userservice.findAll();
        return Result.success(list);
    }
    /**
     * 根据id查询
     * @return
     */
    @GetMapping("/{id}")
    public Result hello(@PathVariable Integer id){
        User user=userservice.findById(id);
        if(user==null)
            return Result.err();
        else
        return Result.success(user);
    }
    /**
     *根据id删除
     ** @return
     */
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        if(userservice.findById(id)==null)
            return Result.err();
        else {
            userservice.delete(id);
            return Result.success();
        }
    }
    /**
     *根据id修改
     ** @return
     */
    @PostMapping("/change")
    public Result change(@RequestBody User user){
        if(userservice.findById(user.getId())==null)
            return Result.err();
        else {
            userservice.change(user);
            return Result.success();
        }
    }
    /**
     * 实行分页查询
     * 传递页码和每页的记录数
     * 获取页表集合和总记录数
     */
    @GetMapping("/page")
    public Result page(Integer size, Integer pageSize){
        PageBean pageBean=userservice.page(size,pageSize);
        return Result.success(pageBean);
    }
    /**
     * 根据性别查询
     */
    @GetMapping("/sex")
    public Result selectBySex(Integer sex){
        List<User> list=userservice.selectBySex(sex);
        return Result.success(list);
    }

}
